﻿using AutoMapper;
using BusinessEntities;
using DataModel;
using DataModel.UnitOfWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using System.Transactions;

namespace BusinessServices
{
    public class RegionServices:IRegionServices
    {
        private readonly UnitOfWork _unitOfWork;
        public RegionServices(UnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        /// <summary>
        /// Fetches product details by id
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public BusinessEntities.RegionEntity GetRegionById(int Id)
        {
            var obj = _unitOfWork.RegionRepository.GetByID(Id);
            if (obj != null)
            {
                Mapper.CreateMap<Region, RegionEntity>();
                var regionModel = Mapper.Map<Region, RegionEntity>(obj);
                return regionModel;
            }
            return null;
        }

        /// <summary>
        /// Fetches all the products.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<BusinessEntities.RegionEntity> GetAllRegions()
        {
            var obj = _unitOfWork.RegionRepository.GetAll().ToList();
            if (obj.Any())
            {
                Mapper.CreateMap<Region, RegionEntity>();
                var model = Mapper.Map<List<Region>, List<RegionEntity>>(obj);
                return model;
            }
            return null;
        }

        /// <summary>
        /// Creates 
        /// </summary>
        /// <param name="productEntity"></param>
        /// <returns></returns>
        public int CreateRegion(BusinessEntities.RegionEntity entity)
        {
            using (var scope = new TransactionScope())
            {
                var obj = new Region
                {
                    RegionName=entity.RegionName,
                    RegionCode=entity.RegionCode,
                    Parent=entity.Parent,
                    Lvl=entity.Lvl
                };
                _unitOfWork.RegionRepository.Insert(obj);
                _unitOfWork.Save();
                scope.Complete();
                return obj.RegionID;
            }
        }

        /// <summary>
        /// Updates 
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="productEntity"></param>
        /// <returns></returns>
        public bool UpdateRegion(int id, BusinessEntities.RegionEntity entity)
        {
            var success = false;
            if (entity != null)
            {
                using (var scope = new TransactionScope())
                {
                    var obj = _unitOfWork.RegionRepository.GetByID(id);
                    if (obj != null)
                    {
                        obj.Lvl = entity.Lvl;
                        obj.Parent = entity.Parent;
                        obj.RegionCode = entity.RegionCode;
                        obj.RegionName = entity.RegionName;

                        _unitOfWork.RegionRepository.Update(obj);
                        _unitOfWork.Save();
                        scope.Complete();
                        success = true;
                    }
                }
            }
            return success;
        }

        /// <summary>
        /// Deletes  
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public bool DeleteRegion(int id)
        {
            var success = false;
            if (id > 0)
            {
                using (var scope = new TransactionScope())
                {
                    var obj = _unitOfWork.RegionRepository.GetByID(id);
                    if (obj != null)
                    {

                        _unitOfWork.RegionRepository.Delete(obj);
                        _unitOfWork.Save();
                        scope.Complete();
                        success = true;
                    }
                }
            }
            return success;
        }
    }
}
